﻿package smooth.core.service.gesture {
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import smooth.core.service.gesture.GestureService;
	import flash.events.TouchEvent;
	
	public class GestureComponent extends Sprite {
		private var gestureService:GestureService;
		private var parentObject:Object;
		private var clickgrabber:Shape = new Shape();	
		private var initialWidth:int;
		private var initialHeight:int;
		private var initialPosX:int;
		private var initialPosY:int;
		private var initialColor:int;
		private var initialAlpha:Number;
		
		public function GestureComponent(parentObject:Object, posX:int, posY:int, sizeX:int, sizeY:int, color:int, alpha:Number) {
			this.parentObject = parentObject;
			this.initialWidth = sizeX;
			this.initialHeight = sizeY;
			this.initialPosX = posX;
 			this.initialPosY = posY;
			this.initialColor = color;
			this.initialAlpha = alpha;
			// Init des visuels du composant			
			this.initGraphics();
			// Init des process du composant
			this.initProcess();
			// Init des events du composant
			this.initEvents();	
		}
		
		private function initGraphics():void {
			var length:Number = this.initialWidth / 8;
			this.x = this.initialPosX;
			this.y = this.initialPosY;
			clickgrabber.graphics.lineStyle(2,0xFFFFFF,1);
			clickgrabber.graphics.beginFill(initialColor,this.initialAlpha);
			clickgrabber.graphics.drawRoundRect(3*length,length,2*length,6*length,5,5);
			clickgrabber.graphics.drawRoundRect(length, 3 * length, 6 * length, 2 * length, 5, 5);
			clickgrabber.graphics.endFill();
			this.alpha = 0;
			this.addChild(clickgrabber);
		}
		
		private function initProcess():void {
			this.gestureService = new GestureService(this.parentObject);
		}
		
		private function initEvents():void {
			this.addEventListener(TouchEvent.MOUSE_DOWN, this.touchDown);						
			this.addEventListener(TouchEvent.MOUSE_UP, this.touchUp);	
		}
		
		private function touchDown(e:TouchEvent):void {
			this.parentObject.noScale = true;
			this.parentObject.noRotate = true;
			this.parentObject.noMove = true;
			this.gestureService.startCatchGesture();
		}
		
		private function touchUp(e:TouchEvent):void {
			this.parentObject.noScale = false;
			this.parentObject.noRotate = false;
			this.parentObject.noMove = false;
			this.gestureService.endCatchGesture();
		}
		
	}
	
}